package com.app.news;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.app.news.util.DB;

public class SignInActivity extends AppCompatActivity {

    private EditText signInUsernameEt;
    private EditText signInPasswordEt;
    private Button signInBtn;
    private String usernameStr;
    private String passwordStr;
    private EditText passwordEd2;
    private String passwordStr2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        initView();
        initEvent();
    }
    private void initView(){
        signInUsernameEt = findViewById(R.id.signin_username_et);
        signInPasswordEt = findViewById(R.id.signin_password_et);
        passwordEd2 = findViewById(R.id.signin_password2_et);
        signInBtn = findViewById(R.id.sign_in_btn);
    }
    private void initData(){
        usernameStr = signInUsernameEt.getText().toString()+"";
        passwordStr = signInPasswordEt.getText().toString()+"";
        passwordStr2 = passwordEd2.getText().toString()+"";
    }
    private void initEvent(){
        signInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                DB db=new DB(SignInActivity.this);
                Boolean result=db.signInInsert(usernameStr,passwordStr);
                if(TextUtils.isEmpty(usernameStr)||TextUtils.isEmpty(passwordStr)||TextUtils.isEmpty(passwordStr2)){
                    Intent intent=new Intent(SignInActivity.this,DialogActivity.class);
                    startActivity(intent);
                }else if (passwordStr==passwordStr2){
                    Toast.makeText(SignInActivity.this,"密码不一致",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SignInActivity.this,"注册成功,前往登录",Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }
}
